/**
 * @param {number[]} code
 * @param {number} k
 * @return {number[]}
 */
var decrypt = function (code, k) {
  let n = code.length
  let ret = new Array(n).fill(0)
  if(k === 0){
    return  ret
  }
  let derection = 1
  if(k<0){
    derection = -1
  }
  for (let i = 0; i < n; i++) {
    let sum = 0
    
    for (let j = 0; j < k * derection; j++) {
       let value
      if (derection > 0) {
          value = i + j + 1 >= n ? (i + j + 1) % n : i + j + 1
      }else{
         value = i - j - 1 < 0 ? (i - j - 1) % n + n : i - j - 1
      }
     
      sum += code[value]
    }
    ret[i] = sum
  }
  return ret
};
// var code = [5, 7, 1, 4],k = 3
// console.log(decrypt(code, k))

// console.log(decrypt([1, 2, 3, 4], 0))

console.log(decrypt([2, 4, 9, 3], -2))
